/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.toggletest;

import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import org.jdom.Element;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ToggleTestConfiguration implements ProjectComponent, JDOMExternalizable {
    List toggles = new ArrayList();

    public static ToggleTestConfiguration getInstance(Project project) {
        return (ToggleTestConfiguration) project.getComponent(ToggleTestConfiguration.class);
    }

    public void readExternal(Element element) throws InvalidDataException {
        toggles.clear();
        List children = element.getChildren("toggle");
        for (Iterator iterator = children.iterator(); iterator.hasNext();) {
            Element child = (Element) iterator.next();
            String from = child.getAttributeValue("from");
            String to = child.getAttributeValue("to");
            toggles.add(new ToggleRow(from, to));
        }
    }

    public void writeExternal(Element element) throws WriteExternalException {
        for (Iterator iterator = toggles.iterator(); iterator.hasNext();) {
            ToggleRow toggleRow = (ToggleRow) iterator.next();
            Element elem = new Element("toggle");
            elem.setAttribute("from", toggleRow.getFrom());
            elem.setAttribute("to", toggleRow.getTo());
            element.addContent(elem);
        }
    }

    public void projectOpened() {
    }

    public void projectClosed() {
    }

    public String getComponentName() {
        return "ToggleTest";
    }

    public void initComponent() {
    }

    public void disposeComponent() {
    }

    public Iterator iterator() {
        return toggles.iterator();
    }

    public List getToggles() {
        ArrayList arrayList = new ArrayList();
        arrayList.addAll(toggles);
        return arrayList;
    }

    public void setToggles(List newToggles) {
        toggles.clear();
        toggles.addAll(newToggles);
    }
}
